jQuery(function($){
	var system_code = $('#search_system_code').val();
	jQuery("#datagrid").jqGrid({
		treeGrid: true,
		treeGridModel: 'adjacency',
		ExpandColumn : 'menu_id',
	    url:'listMenu.action',
	    mtype: "POST",
	    datatype: "json",
	    colNames:['菜单(项)编码', '菜单(项)名称', '序号', 'URL类型', 'URL地址', '已启用', '创建时间', '修改时间'],
	    colModel:[
	       {name:'menu_code',index:'menu_code'},
	       {name:'menu_name',index:'menu_name', width:100},
	       {name:'menu_seq',index:'menu_seq', width:100, align:'center'},
	       {name:'url_type',index:'url_type', width:100, align:'center',
	    	   formatter:function(value,col,record) {
		       		if(value=='0') {
		    			return "无地址";
		    		}
		    		else if(value=='1') {
		    			return "相对地址";
		    		}
		    		else if(value=='2') {
		    			return "绝对地址";
		    		}
		    		else {
		    			return "未知类型";
		    		}
		    	}
	       },
	       {name:'menu_url',index:'menu_url'},
	       {name:'used_flag',index:'used_flag', width:100, align:'center',
	    	   formatter:function(value,col,record) {
		       		if(value=='1') {
		    			return "是";
		    		}
		    		else {
		    			return "否";
		    		}
		    	}
	       },
	       {name:'create_time',index:'create_time', width:100},
	       {name:'modify_time',index:'modify_time', width:100}
	    ],
	    pager: '#pager',
	    rowNum:1000,
	    viewrecords: true,
	    caption: "菜单(项)列表",
	    autowidth: true,
	    height: '100%',
	    ondblClickRow: function(rowid, iRow, iCol, e){
	    	var node=jQuery("#datagrid").getRowData(rowid);
	    	if(node.isLeaf==true || node.isLeaf=='true') {
	    		loadMenuItem(rowid);
	    	}
	    	else {
	    		loadMenu(rowid);
	    	}
	    },
	    serializeGridData: function(data) {
	    	data['menu.system_code']=document.getElementById('search_system_code').value;
	    	data['menu.menu_id']=data.nodeid?data.nodeid:'';
	    	return data;
	    }
	});
	
	jQuery("#datagrid").navGrid(
			'#page',
			{add:false, edit:false, del:true, search:false, refresh:true},
			{},{},
			{
				modal:true,url:"deleteMenu.action",
				beforeSubmit:function(rowid, d, e, f) {
					var node=jQuery("#datagrid").getRowData(rowid);
			    	if(node.isLeaf==true || node.isLeaf=='true') {
			    		this.url='deleteMenuItem.action';
			    	}
			    	else {
			    		this.url='deleteMenu.action';
			    	}
			    	return [true, ''];
				},
				afterSubmit:function(data, params){
					if(data.responseText) {
						var obj = eval('('+ data.responseText +')');
						var msg = obj.message?obj.message:((obj.success==true || obj.success=='true')?"菜单信息删除成功！":"菜单信息删除失败！");
						jQuery("#datagrid").clearGridData();
						return [obj.success, msg];
					} else {
						return [true];
					}
				}
			},{},{}
		);
});


function loadMenu(menu_id) {
	var system_code = $('#search_system_code').val();
	showMDialog('loadMenu.action?edit=true&menu.menu_id='+menu_id+'&menu.system_code='+system_code,'dialogWidth=850px;dialogHeight=600px');
//	window.open('loadMenu.action?edit=true&menu.menu_id='+menu_id+'&menu.system_code='+system_code);
}

function loadMenuItem(menuitem_id) {
	var system_code = $('#search_system_code').val();
	showMDialog('loadMenuItem.action?edit=true&menuitem.menuitem_id='+menuitem_id+'&menu.system_code='+system_code,'dialogWidth=850px;dialogHeight=600px');
//	window.open('loadMenuItem.action?edit=true&menuitem.menuitem_id='+menuitem_id+'&menu.system_code='+system_code);
}

function gridReload(){
	jQuery("#datagrid").trigger("reloadGrid");
}